#include <stdio.h>
#include <linux/input.h>

#include "key_input.h"
#include "ubuntu_util.h"
#include "plat_input.h"

//按键设备路径
#define UBUNTU_KEYBOARD_DEV_PATH "/dev/input/event1"
//按键按住时连续触发事件间隔(为0表示不触发)
#define UBUNTU_KEYBOARD_INTERVAL_MS PLAT_INPUT_INTERVAL_MS
//最大同时按住按键数量(当上面 INTERVAL_MS > 0 时才有意义)
#define UBUNTU_KEYBOARD_MAX_KEY_DOWN 10

#define INPUT_INFO(...) fprintf(stdout, "\033[32m[INPUT_INFO] %s(%d): \033[0m", __FUNCTION__, __LINE__),fprintf(stdout, __VA_ARGS__)
#define INPUT_ERR(...) fprintf(stderr, "\033[31m[INPUT_ERR] %s(%d): \033[0m", __FUNCTION__, __LINE__),fprintf(stderr, __VA_ARGS__)

class PlatInput::Param {
public:
    void *keyPriv = NULL;
    void *touchPriv = NULL;
    PlatKeyCallback keyCallback = NULL;
    PlatTouchCallback touchCallback = NULL;
    bool keyEnable = false;
    bool touchEnable = false;
};

static void _PlatKeyCallback(void *priv, int32_t key, int32_t value)
{
    PlatInput::Param *param = (PlatInput::Param*)priv;
    PlatInputKey keyType;
    PlatInputState keyState;

    // INPUT_INFO("key: %d, value: %d \r\n", key, value);

    switch(key)
    {
    case KEY_W: keyType = PIK_UP; break;
    case KEY_S: keyType = PIK_DOWN; break;
    case KEY_A: keyType = PIK_LEFT; break;
    case KEY_D: keyType = PIK_RIGHT; break;

    case KEY_B: keyType = PIK_BACK; break;
    case KEY_SPACE: keyType = PIK_HOME; break;

    case KEY_Q: keyType = PIK_UP_LEFT; break;
    case KEY_E: keyType = PIK_UP_RIGHT; break;
    case KEY_Z: keyType = PIK_DOWN_LEFT; break;
    case KEY_X: keyType = PIK_DOWN_RIGHT; break;

    case KEY_UP: keyType = PIK_ARROW_UP; break;
    case KEY_DOWN: keyType = PIK_ARROW_DOWN; break;
    case KEY_LEFT: keyType = PIK_ARROW_LEFT; break;
    case KEY_RIGHT: keyType = PIK_ARROW_RIGHT; break;

    default: return;
    }

    switch(value)
    {
    case 0: keyState = PIS_UP; break;
    case 1: keyState = PIS_DOWN; break;
    case 2: keyState = PIS_HOLD; break;
    default: return;
    }

    if (param->keyCallback)
        param->keyCallback(param->keyPriv, keyType, keyState);
}

PlatInput::PlatInput()
{
    param = new PlatInput::Param();
}

PlatInput::~PlatInput()
{
    KeyInputDeInit();
    delete param;
}

void PlatInput::SetKeyCallback(void *priv, PlatKeyCallback callback)
{
    param->keyPriv = priv;
    param->keyCallback = callback;
}

void PlatInput::SetTouchCallback(void *priv, PlatTouchCallback callback)
{
    param->touchPriv = priv;
    param->touchCallback = callback;
}

void PlatInput::KeyCallbackEnable(bool enable)
{
    if (param->keyEnable != enable)
    {
        if (enable)
        {
            if (KeyInputInit(
                UBUNTU_KEYBOARD_DEV_PATH,
                UBUNTU_KEYBOARD_INTERVAL_MS,
                UBUNTU_KEYBOARD_MAX_KEY_DOWN,
                param,
                &_PlatKeyCallback) != 0)
            {
                return;
            }
        }
    }
    param->keyEnable = enable;
}

void PlatInput::TouchCallbackEnable(bool enable)
{
    param->touchEnable = enable;
}
